import { Router, Request, Response } from 'express';
import {
  createPaymentIntent,
  createSetupIntent,
  listPaymentMethods,
  attachPaymentMethod,
  detachPaymentMethod,
  setDefaultPaymentMethod,
} from '../controllers/stripe.controller';

const router = Router();

/**
 * POST /api/payment/create-intent
 * Create a payment intent for one-time payments
 */
router.post('/create-intent', async (req: Request, res: Response) => {
  try {
    await createPaymentIntent(req, res);
  } catch (error) {
    console.error('Error in create-intent route:', error);
    res.status(500).json({ error: 'Failed to create payment intent' });
  }
});

/**
 * POST /api/payment/create-setup-intent
 * Create a setup intent for saving payment methods
 */
router.post('/create-setup-intent', async (req: Request, res: Response) => {
  try {
    await createSetupIntent(req, res);
  } catch (error) {
    console.error('Error in create-setup-intent route:', error);
    res.status(500).json({ error: 'Failed to create setup intent' });
  }
});

/**
 * GET /api/payment/methods/:customerId
 * List all payment methods for a customer
 */
router.get('/methods/:customerId', async (req: Request, res: Response) => {
  try {
    await listPaymentMethods(req, res);
  } catch (error) {
    console.error('Error in list payment methods route:', error);
    res.status(500).json({ error: 'Failed to list payment methods' });
  }
});

/**
 * POST /api/payment/attach-method
 * Attach a payment method to a customer
 */
router.post('/attach-method', async (req: Request, res: Response) => {
  try {
    await attachPaymentMethod(req, res);
  } catch (error) {
    console.error('Error in attach payment method route:', error);
    res.status(500).json({ error: 'Failed to attach payment method' });
  }
});

/**
 * DELETE /api/payment/detach-method/:paymentMethodId
 * Detach (delete) a payment method
 */
router.delete('/detach-method/:paymentMethodId', async (req: Request, res: Response) => {
  try {
    await detachPaymentMethod(req, res);
  } catch (error) {
    console.error('Error in detach payment method route:', error);
    res.status(500).json({ error: 'Failed to detach payment method' });
  }
});

/**
 * POST /api/payment/methods/set-default
 * Set a payment method as default for a customer
 */
router.post('/methods/set-default', async (req: Request, res: Response) => {
  try {
    await setDefaultPaymentMethod(req, res);
  } catch (error) {
    console.error('Error in set default payment method route:', error);
    res.status(500).json({ error: 'Failed to set default payment method' });
  }
});

export default router;
